package com.kyomonn.talkischeap.CodingResources;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TabHost;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.kyomonn.talkischeap.R;


public class CodingResourcesFragment extends Fragment {

    private CodingResourcesViewModel viewModel;
    private ViewGroup root;

    public static CodingResourcesFragment Create() {
        return new CodingResourcesFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        root = container;
        return inflater.inflate(R.layout.fragment_coding_resources, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewModel = new ViewModelProvider(this).get(CodingResourcesViewModel.class);
        viewModel.Init();

        TabHost tabHost = view.findViewById(R.id.tabHost);
        tabHost.setup();

        TabHost.TabSpec tab1 = tabHost.newTabSpec("news");
        tab1.setIndicator("资讯");
        tab1.setContent(tag -> initNewsTab());

        TabHost.TabSpec tab2 = tabHost.newTabSpec("tutorial");
        tab2.setIndicator("教程");
        tab2.setContent(tag -> initTutorialTab());

        TabHost.TabSpec tab3 = tabHost.newTabSpec("books");
        tab3.setIndicator("图书");
        tab3.setContent(tag -> initBooksTab());

//        TabHost.TabSpec tab4 = tabHost.newTabSpec("document");
//        tab4.setIndicator("文档");
//        tab4.setContent(tag -> initDocumentTab());

        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
//        tabHost.addTab(tab4);
    }

    private View initNewsTab() {
        View view = LayoutInflater.from(requireContext()).inflate(R.layout.tab_news, root, false);

        ListView rvNews = view.findViewById(R.id.rv_news);
        rvNews.setAdapter(new CodingResourcesItemAdapter(requireContext(), R.layout.item_news, viewModel.newsTitles, viewModel.newsUrls));

        ListView rvDaily = view.findViewById(R.id.rv_daily_coding);
        rvDaily.setAdapter(new CodingResourcesItemAdapter(requireContext(), R.layout.item_news, viewModel.dailyCodingTitles, viewModel.dailyCodingUrls));
        return view;
    }

    private View initTutorialTab() {
        View view = LayoutInflater.from(requireContext()).inflate(R.layout.tab_tutorial, root, false);

        GridView gvCCpp = view.findViewById(R.id.gv_c_cpp);
        GridView gvCSharp = view.findViewById(R.id.gv_csharp);
        GridView gvJava = view.findViewById(R.id.gv_java);
        GridView gvPython = view.findViewById(R.id.gv_python);
        GridView gvGo = view.findViewById(R.id.gv_go);
        GridView gvKotlin = view.findViewById(R.id.gv_kotlin);
        GridView gvRust = view.findViewById(R.id.gv_rust);

        gvCCpp.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_c_cpp), viewModel.tutorialUrls.get(R.id.gv_c_cpp)));
        gvCSharp.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_csharp), viewModel.tutorialUrls.get(R.id.gv_csharp)));
        gvJava.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_java), viewModel.tutorialUrls.get(R.id.gv_java)));
        gvPython.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_python), viewModel.tutorialUrls.get(R.id.gv_python)));
        gvGo.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_go), viewModel.tutorialUrls.get(R.id.gv_go)));
        gvKotlin.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_kotlin), viewModel.tutorialUrls.get(R.id.gv_kotlin)));
        gvRust.setAdapter(new TutorialTabButtonAdapter(requireContext(), R.layout.item_tutorial, viewModel.tutorialTitles.get(R.id.gv_rust), viewModel.tutorialUrls.get(R.id.gv_rust)));

        return view;
    }

    private View initBooksTab() {
        View view = LayoutInflater.from(requireContext()).inflate(R.layout.tab_books, root, false);

        GridView gvBooks = view.findViewById(R.id.gv_books);
//        gvBooks.setAdapter(new BookTabButtonAdapter(requireContext(), R.layout.item_book, viewModel.newsTitles, viewModel.newsUrls));

        return view;
    }

    private View initDocumentTab() {
        return LayoutInflater.from(requireContext()).inflate(R.layout.tab_document, root, false);
    }
}